﻿Imports System.IO

Public Class 品牌型号

#Region "外观处理"

    '窗体大小变化
    Private Sub 品牌型号_Resize(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Resize
        If Me.Width < 569 Then
            Me.Width = 569
        End If
        If Me.Height < 486 Then
            Me.Height = 486
        End If
    End Sub

#End Region

#Region "添加操作页"

    '添加品牌按扭
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim addname As String = Trim(TextBox1.Text)

        '填定内容判空
        If addname = "" Then
            MsgBox("请输入添加品牌的名称")
            Label14.Visible = False
            TextBox1.Focus()
            Exit Sub
        End If

        If RecordSetExist("品牌表", "品牌", addname) Then
            MsgBox("品牌名称己存在")
            Label14.Visible = False
            TextBox1.Text = ""
            TextBox1.Focus()
        Else
            Dim sqlstr As String = "insert into 品牌表(品牌) values('" + addname + "')"
            SQLupdata(sqlstr)
            Label14.Text = "品牌'" + addname + "'己成功添加！"
            Label14.Visible = True
            TextBox1.Text = ""
            TextBox1.Focus()
        End If

    End Sub
    Private Sub TextBox1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyDown

        If e.KeyCode = Keys.Enter Then
            Button1_Click(sender, e)
        End If

    End Sub

    '添加型号按扭
    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Dim PP As String = Trim(ComboBox1.Text)
        Dim XH As String = Trim(TextBox2.Text)
        Dim JG As String = Trim(TextBox6.Text)

        '填定内容判空
        If PP = "" Or PP = "请选择.." Then
            MsgBox("请选择要添加型号所属品牌！")
            Label15.Visible = False
            ComboBox1.Focus()
            Exit Sub
        End If

        If XH = "" Then
            MsgBox("请输入添加型号！")
            Label15.Visible = False
            TextBox2.Focus()
            Exit Sub
        End If

        If JG = "" Then
            MsgBox("请输入所添加型号的一般售价！")
            Label15.Visible = False
            TextBox6.Focus()
            Exit Sub
        End If


        '品牌是否存在
        If Not RecordSetExist("品牌表", "品牌", PP) Then
            MsgBox("品牌不存在，请重新请择")
            Label15.Visible = False
            ComboBox1.Focus()
            Exit Sub
        Else
            Dim sqlstr As String = "select * from 型号表 where 所属品牌 ='" + PP + "' and 型号 ='" + XH + "'"
            Dim temset As Data.DataSet = SQLtoDataSet(sqlstr)
            If temset.Tables(0).Rows.Count > 0 Then
                MsgBox("品牌'" + PP + "'的机型'" + XH + "'己存在！")
                Label15.Visible = False
                TextBox2.Focus()
                Exit Sub
            End If
        End If

        '价格数值的判断
        If Not IsNumeric(JG) Then
            MsgBox("你输入的价格不是一个正确的数字，请重新输入！")
            Label15.Visible = False
            TextBox6.Focus()
            Exit Sub
        End If

        '是否添加数图片,复制
        Dim picroute As String = Trim(TextBox3.Text)
        If picroute <> "" And IO.File.Exists(picroute) Then
            File.Copy(picroute, apps + "\PIC\" + PP + "_" + XH + ".jpg")
            picroute = apps + "\PIC\" + PP + "_" + XH + ".jpg"
            If picroute.Length > 250 Then
                picroute = ""
            End If
        End If

        '执行数据插入
        Dim str As String = "insert into 型号表(型号,所属品牌,一般售价,图片地址,备注) values('" + XH + "','" + PP + "','" + JG + "','" + picroute + "','" + TextBox4.Text + "')"
        SQLupdata(str)
        Label15.Text = "型号'" + XH + "'添加成功！"
        Label15.Visible = True
        TextBox2.Text = ""
        TextBox3.Text = ""
        TextBox4.Text = ""
        TextBox6.Text = ""
        TextBox2.Focus()

    End Sub
    Private Sub TextBox6_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox6.KeyDown
        If e.KeyCode = Keys.Enter Then
            Button2_Click(sender, e)
        End If
    End Sub

    '浏览按扭
    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        OpenFileDialog1.Filter = "图片文件(*.jpg,*.gif,*.bmp)|*.jpg;*.gif;*.bmp"
        OpenFileDialog1.Multiselect = False
        OpenFileDialog1.FileName = ""
        If OpenFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
            TextBox3.Text = OpenFileDialog1.FileName
            PictureBox1.ImageLocation = OpenFileDialog1.FileName
        End If

    End Sub

    '更新品牌选择框
    Private Sub ComboBox1_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles ComboBox1.GotFocus

        ComboBox1.Items.Clear()
        Dim sqlstr As String = "select 品牌 from 品牌表"
        Dim datasettem As Data.DataSet = SQLtoDataSet(sqlstr)
        If datasettem.Tables(0).Rows.Count > 0 Then
            For i = 0 To datasettem.Tables(0).Rows.Count - 1
                ComboBox1.Items.Add(datasettem.Tables(0).Rows(i).Item(0).ToString)
            Next
        End If

    End Sub

#End Region

#Region "修改操作页"

    Dim editflag As Boolean = False    '是正在修改

    '选择页2时触发事件
    Private Sub TabControl1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles TabControl1.Click
        treeshow()
    End Sub
    Private Sub treeshow()
        TreeView1.Enabled = False
        TreeView1.Height = 380
        TreeView1.Nodes.Clear()

        Dim sql1 As String = "select 品牌 from 品牌表"
        Dim set1 As Data.DataSet = SQLtoDataSet(sql1)
        Dim sql2 As String = "select 型号,所属品牌,一般售价,备注 from 型号表"
        Dim set2 As Data.DataSet = SQLtoDataSet(sql2)

        '品牌节点
        For i = 0 To set1.Tables(0).Rows.Count - 1
            Dim pp As String = set1.Tables(0).Rows(i).Item(0).ToString
            TreeView1.Nodes.Add(pp, pp, 0)
            TreeView1.Nodes(pp).Tag = "1"
        Next

        '型号节点
        For i = 0 To set2.Tables(0).Rows.Count - 1
            Dim temdata As Data.DataRow = set2.Tables(0).Rows(i)
            Dim XH As String = temdata.Item(0).ToString
            Dim pp As String = temdata.Item(1).ToString
            TreeView1.Nodes.Item(pp).Nodes.Add(XH, XH, 1)

            Dim tagstr As String = "2|" + temdata.Item("所属品牌") + "|" + temdata.Item("一般售价").ToString + "|" + temdata.Item("备注")
            TreeView1.Nodes(pp).Nodes(XH).Tag = tagstr
        Next

        '控件内点清零
        controlerClear()
        TreeView1.Enabled = True

    End Sub
    Private Sub controlerClear()
        Label9.Text = ""
        Label10.Text = ""
        Label16.Text = ""
        TextBox5.Text = ""
        PictureBox2.ImageLocation = apps + "\skin\nopic.jpg"
    End Sub

    '点击树形目录
    Private Sub TreeView1_AfterSelect(ByVal sender As System.Object, ByVal e As System.Windows.Forms.TreeViewEventArgs) Handles TreeView1.AfterSelect
        Dim PPnode As TreeNode = TreeView1.SelectedNode
        Dim showtag() As String = PPnode.Tag.ToString.Trim.Split("|")
        If showtag(0) = "1" Then
            PPnode.ImageIndex = 0
            controlerClear()
            Label9.Text = PPnode.Name
        Else
            PPnode.ImageIndex = 1
            controlerClear()
            Label10.Text = PPnode.Name
            Label9.Text = showtag(1)
            Label16.Text = showtag(2) + " 元/台"
            TextBox5.Text = showtag(3)

            Dim filena As String = apps + "\PIC\" + showtag(1) + "_" + PPnode.Name + ".jpg"
            If File.Exists(filena) Then
                PictureBox2.ImageLocation = filena
            End If

        End If

    End Sub

    '控件使能
    Private Sub Enable_All()
        TreeView1.Enabled = False
        TextBox7.Visible = True
        Dim PPnode As TreeNode = TreeView1.SelectedNode
        Dim showtag() As String = PPnode.Tag.ToString.Trim.Split("|")
        TextBox7.Text = showtag(2).ToString
        Label18.Visible = True
        TextBox5.ReadOnly = False
        PictureBox2.Enabled = True
        PictureBox2.Cursor = Cursors.Hand
        Button4.Visible = True
        Button5.Visible = True
        TreeView1.Height = 348
        TextBox7.Focus()
    End Sub
    Private Sub EnableNone()
        TreeView1.Enabled = True
        TextBox7.Visible = False
        Label18.Visible = False
        TextBox5.ReadOnly = True
        PictureBox2.Enabled = False
        PictureBox2.Cursor = Cursors.Default
        Button4.Visible = False
        Button5.Visible = False
        TreeView1.Height = 380
        TreeView1.Focus()
    End Sub

    '取消
    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
        editflag = False
        EnableNone()
    End Sub

    '修改资料
    Private Sub 修改ToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles 修改ToolStripMenuItem.Click

        editflag = True
        Enable_All()

    End Sub

    '右键菜单项使能
    Private Sub TreeView1_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles TreeView1.MouseUp

        '右键
        If e.Button = Windows.Forms.MouseButtons.Right Then

            If Not TreeView1.SelectedNode.IsSelected Then

                修改ToolStripMenuItem.Enabled = False
                删除ToolStripMenuItem.Enabled = False
                Exit Sub

            End If


            Dim PPnode As TreeNode = TreeView1.SelectedNode
            Dim showtag() As String = PPnode.Tag.ToString.Trim.Split("|")

            If showtag(0) = "1" Then
                修改ToolStripMenuItem.Enabled = False
                删除ToolStripMenuItem.Enabled = True
            Else
                修改ToolStripMenuItem.Enabled = True
                删除ToolStripMenuItem.Enabled = True
            End If

        End If

    End Sub

    '确定修改
    Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click

        Dim JG As String = TextBox7.Text
        Dim BZ As String = TextBox5.Text

        '价格数值的判断
        If Not IsNumeric(JG) Then
            MsgBox("你输入的价格不是一个正确的数字，请重新输入！")
            TextBox7.Focus()
            Exit Sub
        End If

        Dim PPnode As TreeNode = TreeView1.SelectedNode
        Dim showtag() As String = PPnode.Tag.ToString.Trim.Split("|")

        TreeView1.SelectedNode.Tag = showtag(0) + "|" + showtag(1) + "|" + JG + "|" + BZ
        Label16.Text = JG + " 元/台"

        '修改数据库
        Dim sqlstr As String = "update 型号表 "
        sqlstr += "set 一般售价 = '" + JG + "',备注 = '" + BZ + "' where 型号 ='" + Label10.Text + "' and 所属品牌 ='" + Label9.Text + "'"
        SQLupdata(sqlstr)

        '控制显隐
        editflag = False
        EnableNone()

    End Sub

    '删除品牌/型号
    Private Sub 删除ToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles 删除ToolStripMenuItem.Click

        Dim PPnode As TreeNode = TreeView1.SelectedNode
        Dim PP As String = PPnode.Text
        Dim showtag() As String = PPnode.Tag.ToString.Trim.Split("|")

        If showtag(0) = "1" Then
            '删除品牌
            Dim i = MsgBox("你确定要删除品牌'" + PP + "'的信息吗？（包括其对应的所有商品型号）", MsgBoxStyle.YesNo, "警告")
            If i = MsgBoxResult.Yes Then

                Dim sql1 As String = "DELETE FROM 品牌表 WHERE 品牌 = '" + PP + "'"
                SQLupdata(sql1)

                Debug.Print("删除品牌成功")

                '删除品牌对应型号
                Dim sql2 As String = "select 型号,所属品牌 from 型号表 where 所属品牌='" + PP + "'"
                Dim ds As Data.DataSet = SQLtoDataSet(sql2)
                For i = 0 To ds.Tables(0).Rows.Count - 1

                    Dim filen As String = ds.Tables(0).Rows(i).Item("所属品牌").ToString + "_" + ds.Tables(0).Rows(i).Item("型号").ToString + ".jpg"
                    Dim pic As String = apps + "\PIC\" + filen

                    TreeView1.Nodes.Item(PP).Nodes.Item(ds.Tables(0).Rows(i).Item("型号").ToString).Remove()

                    '删除图片
                    DelPic(pic)

                    '删除记录
                    Dim sql3 As String = "DELETE FROM 型号表 WHERE 型号 = '" + ds.Tables(0).Rows(i).Item("型号").ToString + "' and 所属品牌='" + ds.Tables(0).Rows(i).Item("所属品牌").ToString + "'"
                    SQLupdata(sql3)
                Next
                TreeView1.Nodes.Item(PP).Remove()

            End If
        Else
            '删除型号
            Dim i = MsgBox("你确定要型号‘" + PP + "'信息吗？", MsgBoxStyle.YesNo, "警告")
            If i = MsgBoxResult.Yes Then
                Dim sql As String = "DELETE FROM 型号表 WHERE 型号 = '" + PP + "' and 所属品牌='" + Label9.Text + "'"
                SQLupdata(sql)
                PPnode.Remove()

                Dim pic As String = apps + "\PIC\" + Label9.Text + "_" + PP + ".jpg"

                '删除图片
                DelPic(pic)

            End If

        End If

    End Sub

    '删除图片
    Private Sub DelPic(ByVal dir As String)
        If File.Exists(dir) Then
            File.Delete(dir)
        End If
    End Sub

    '点击图片
    Private Sub PictureBox2_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox2.MouseUp
        If e.Button = Windows.Forms.MouseButtons.Left Then
            '浏览图片
            Dim nl As New OpenFileDialog
            nl.Filter = "图片文件(*.jpg,*.gif,*.bmp)|*.jpg;*.gif;*.bmp"
            nl.Multiselect = False
            nl.FileName = ""
            If nl.ShowDialog = Windows.Forms.DialogResult.OK Then
                PictureBox2.ImageLocation = nl.FileName
                File.Copy(nl.FileName, apps + "\PIC\" + Label9.Text + "_" + Label10.Text + ".jpg", True)
            End If

        ElseIf e.Button = Windows.Forms.MouseButtons.Right Then
            '删除图片
            Dim pic As String = PictureBox2.ImageLocation
            DelPic(pic)
            PictureBox2.ImageLocation = apps + "\skin\nopic.jpg"

        End If

    End Sub

#End Region

#Region "全局操作"

    '关闭窗体
    Private Sub 品牌型号_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        PinPaiform = Nothing
    End Sub

    '窗体加载
    Private Sub 品牌型号_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        PictureBox1.Image = Image.FromFile(apps + "\skin\nopic.jpg")

    End Sub
    Private Sub 品牌型号_Shown(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Shown
        TextBox1.Focus()
    End Sub

    '价格栏只能输入数字
    Private Sub TextBox6_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox6.KeyPress
        If e.KeyChar >= "0" And e.KeyChar <= "9" Or e.KeyChar = Chr(Keys.Back) Or e.KeyChar = Chr(46) Then  '只允许输入数字与点
            e.Handled = False           '当按键不合法时　则不允许输入
        Else
            e.Handled = True            '当按键合法时　则允许输入并显示
        End If
    End Sub
    Private Sub TextBox7_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox7.KeyPress
        If e.KeyChar >= "0" And e.KeyChar <= "9" Or e.KeyChar = Chr(Keys.Back) Or e.KeyChar = Chr(46) Then  '只允许输入数字与点
            e.Handled = False           '当按键不合法时　则不允许输入
        Else
            e.Handled = True            '当按键合法时　则允许输入并显示
        End If
    End Sub

#End Region

End Class